//
//  MyViewController.m
//  UI豆瓣2
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MyViewController.h"
#import "MyMovieVC.h"
#import "LoginViewController.h"
#import "SDImageCache.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *myTableView;

@end

@implementation MyViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    //获取当前是否登陆的状态,若为1 说明是登陆,就显示"注销"按钮;若不为1 说明是未登录状态,就显示"登陆"按钮
    NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
    NSString *isLogin = [loginUserDef objectForKey:@"isLogin"];
    if ([isLogin isEqualToString:@"1"]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(logoutAction:)];
    }else {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(logoutAction:)];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    
    //去掉多余cell
    self.myTableView.tableFooterView = [UIView new];
    //去掉cell分割线
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_myTableView release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(logoutAction:)];
    
    self.navigationItem.title = @"我的";
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的电影";
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"我的活动";
    }else if (indexPath.row == 2){
        cell.textLabel.text = @"清除缓存";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
    //获取登录状态,如果登陆了,就可以看到收藏的东西,如果未登录,就会弹出提示框提示未登录,不会进到收藏界面
    NSString *name = [loginUserDef stringForKey:@"isLogin"];
    
    if ([name isEqualToString:@"1"]) {
        
        if (indexPath.row == 0) {
            MyMovieVC *myMovie = [[MyMovieVC alloc] init];
            [self.navigationController pushViewController:myMovie animated:YES];
            [myMovie release];
        }else if (indexPath.row == 2){
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"清除缓存成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *clear = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"清除缓存");
            }];
            [alert addAction:clear];
            [self presentViewController:alert animated:YES completion:nil];
            [[SDImageCache sharedImageCache] clearDisk];
            
        }
        
    }else{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"亲,你还没有登陆哦!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"取消");
            
        }];
        UIAlertAction *login = [UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"登陆");
            
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
        }];
        [alert addAction:canact];
        [alert addAction:login];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
}


- (void)logoutAction:(UIBarButtonItem *)rightBtn{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
    if ([loginUserDef stringForKey:@"isLogin"] != nil) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"是否注销" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"取消");
        }];
        UIAlertAction *othact = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"OK");
            
            [loginUserDef removeObjectForKey:@"isLogin"];
            
            UIAlertController *alertOut = [UIAlertController alertControllerWithTitle:@"注销" message:@"注销成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *canactOut = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"取消");
                [self.navigationController pushViewController:loginVC animated:YES];
                
            }];
            [alertOut addAction:canactOut];
            [self presentViewController:alertOut animated:YES completion:nil];
        }];
        [alert addAction:canact];
        [alert addAction:othact];
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }else{
        [self.navigationController pushViewController:loginVC animated:YES];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
